const pagination = require('mongoose-sex-page');
const { Article } = require('../../model/article');
const { Category } = require('../../model/Category');
module.exports = async (req,res)=>{
  const regex = new RegExp(escapeRegex(req.body.key), 'gi');
  let category = await Category.find();
  let articles = await Article.find({title: regex});
  if (articles.length != 0){
    res.render('home/search',{
      articles,
      category,
      message: '搜索到你可能要的结果如下：'
    });
  }else{
    res.render('home/search',{
      message: '查询无结果！！！'
    });
  }
}
function escapeRegex(text) {
  return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
};